﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicLibrary.Model;
using MedicLibrary.Controler;

namespace MedicLibrary.View
{
    public partial class frmDSNhomThuoc : Form
    {
        public String action;

        public delegate void getNhomCha(int ID, String fname);

        public getNhomCha getNC;

        public frmDSNhomThuoc()
        {
            InitializeComponent();
        }

        private void frmDSNhomThuoc_Load(object sender, EventArgs e)
        {
            dgvDSNhomThuoc.DataSource = NhomThuoc.getDanhSachNhomThuoc("");
            NhomThuoc a = new NhomThuoc();
        }

        private void dgvDSNhomThuoc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.action == "LIST")
            {
                Session.IDNhomThuoc = Int32.Parse(dgvDSNhomThuoc.SelectedRows[0].Cells["ID"].Value.ToString());
                frmChiTietNhomThuoc frmCTNT = new frmChiTietNhomThuoc();
                frmCTNT.applyAction("VIEW");
                frmCTNT.Show();
                this.Close();
            }
            else if (this.action == "SEARCH")
            {   
                //Session.IDNhomCha = Int32.Parse(dgvDSNhomThuoc.SelectedRows[0].Cells["ID"].Value.ToString());
                if (Int32.Parse(dgvDSNhomThuoc.SelectedRows[0].Cells["ID"].Value.ToString()) == Controler.Session.IDNhomThuoc)
                    MessageBox.Show("Không thể chọn chính mình làm nhóm thuốc cha!");
                else
                {
                        getNC(Int32.Parse(dgvDSNhomThuoc.SelectedRows[0].Cells["ID"].Value.ToString()), dgvDSNhomThuoc.SelectedRows[0].Cells["TenNhom"].Value.ToString());
                        this.Close();
                }
            }
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmChiTietNhomThuoc ctNhom = new frmChiTietNhomThuoc();
            ctNhom.applyAction("ADD");
            ctNhom.ShowDialog();
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Session.IDNhomThuoc = Int32.Parse(dgvDSNhomThuoc.SelectedRows[0].Cells["ID"].Value.ToString());
            frmChiTietNhomThuoc ctNhom = new frmChiTietNhomThuoc();
            ctNhom.applyAction("EDIT");
            ctNhom.ShowDialog();
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            NhomThuoc nt = new NhomThuoc();
            nt.ID = Int32.Parse(dgvDSNhomThuoc.SelectedRows[0].Cells["ID"].Value.ToString());
            nt.deleteNhomThuoc();

            dgvDSNhomThuoc.DataSource = NhomThuoc.getDanhSachNhomThuoc("");
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            dgvDSNhomThuoc.DataSource = NhomThuoc.getDanhSachNhomThuoc(txtTimKiem.Text);
        }

        private void btnXemHet_Click(object sender, EventArgs e)
        {
            dgvDSNhomThuoc.DataSource = NhomThuoc.getDanhSachNhomThuoc("");
            txtTimKiem.Text = "";
        }

        public void applyAction(String ac)
        {
            this.action = ac;
            bool status = false;
            if (this.action == "LIST")
            {
                status = true;
            }
            else if (this.action == "SEARCH")
            {
                status = false;
            }
            this.btnThem.Enabled = status;
            this.btnXoa.Enabled = status;
            this.btnSua.Enabled = status;
        }

        private void txtTimKiem_KeyUp(object sender, KeyEventArgs e)
        {
            dgvDSNhomThuoc.DataSource = NhomThuoc.getDanhSachNhomThuoc(txtTimKiem.Text);
        }
    }
}
